package com.atguigu.java;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * RandomAccessFile的使用
 * 1.RandomAccessFile直接继承于java.lang.Object类，实现了DataInput和
 * DataOutput接口
 * 2.RandomAccessFile既可以作为一个输入流，又可以作为提个输出流
 *
 * 3.如果RandomAccessFile作为一个输出流时，写出到的文件不存在，则在执行过程中自动创建
 *   如果写出到的文件，则会对原有文件进行覆盖，而且是从头覆盖
 *   例：如果源文件是：abcdefghijkl
 *      输出流输出：xyz
 *      则运行结果为：xyzdefghijkl
 *
 * 4.可以通过相关的操作，实现RandomAccessFile的“插入”操作，（test3）
 *
 */
public class RandomAccessFileTest {

    @Test
    public void test1(){

        RandomAccessFile raf1 = null;
        RandomAccessFile raf2 = null;
        try {
            raf1 = new RandomAccessFile(new File("下午 趴在桌子的女孩4k动漫壁纸3840x2160_彼岸图网.jpg"),"r");
            raf2 = new RandomAccessFile(new File("下午 趴在桌子的女孩4k动漫壁纸3840x2160_彼岸图网1.jpg"),"rw");

            byte[] buffer = new byte[20];
            int len;
            while((len = raf1.read(buffer)) != -1){
                raf2.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(raf1 != null) {
                try {
                    raf1.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(raf2 != null) {
                try {
                    raf2.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void test2(){

        RandomAccessFile raf1 = null;
        try {
            raf1 = new RandomAccessFile(new File("hello.txt"),"rw");
            //将def覆盖了
            raf1.seek(3);//将指针调到角标为3的位置
            raf1.write("xyz".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(raf1 != null) {
                try {
                    raf1.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /*
    使用RandomAccessFile实现数据的插入效果
     */
    @Test
    public void test3(){

        RandomAccessFile raf1 = null;
        try {
            raf1 = new RandomAccessFile(new File("hello.txt"),"rw");

            StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
            raf1.seek(3);//将指针调到角标为3的位置
            //保存指针3后面的所有数据到StringBuilder中
            byte[] buffer = new byte[20];
            int len;
            while ((len = raf1.read(buffer)) != -1){
                builder.append(new String(buffer,0,len));
            }
            //调回指针，写入“xyz”
            raf1.seek(3);
            raf1.write("xyz".getBytes());

            raf1.write(builder.toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(raf1 != null) {
                try {
                    raf1.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
